package com.corejsf;

import java.util.Properties;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

/**
 * This class loads the configurations.properties file into a Properties
 * object and allows for searching of configuration values by key.
 *
 * @author Vukasin Simic
 * @version 1.0
 * @link http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html?page=2
 */
@Named("configurations")
@ApplicationScoped
public class Configurations implements Serializable {
    /**
    * serialVersionUID.
    */
    private static final long serialVersionUID = -6296079634315079010L;
    /**
    * Properties object containing application configurations.
    */
    private Properties config;

    /**
     * Constructor.
     */
    public Configurations() {
        config = new Properties();
        try {
             config.load(this.getClass().
                     getResourceAsStream("configurations.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets configuration value associated with provided key.
     *
     * @param key Configuration key with corresponding String value
     * @return configuration value corresponding to provided key
     */
    public String getConfig(final String key) {
        return config.getProperty(key);
    }
}